package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.AdListBO;
import com.cskaoyan.bean.bo.CouponCreateBO;
import com.cskaoyan.bean.pojo.MarketCoupon;
import com.cskaoyan.bean.vo.CouponListVO;
import com.cskaoyan.bean.vo.CouponListuserVO;
import com.cskaoyan.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券管理
 *
 * @author chenxu
 * @since 2022/04/06 23:25
 */
@RestController
@RequestMapping("admin")
public class CouponController {
    @Autowired
    CouponService couponService;

    //查询优惠券信息
    @RequestMapping("coupon/list")
    public BaseRespVo list(AdListBO adListBO){
        CouponListVO couponListVO = couponService.list(adListBO);
        return BaseRespVo.ok(couponListVO);
    }
    //删除优惠券信息
    @PostMapping("coupon/delete")
    public BaseRespVo delete(@RequestBody MarketCoupon marketCoupon){
        couponService.delete(marketCoupon.getId());
        return BaseRespVo.ok();
    }
    //读取优惠券详细信息（详情）
    @RequestMapping("coupon/read")
    public BaseRespVo read(Integer id){
        MarketCoupon marketCoupon = couponService.read(id);
        return BaseRespVo.ok(marketCoupon);
    }

    //获取优惠券使用的用户信息
    @RequestMapping("coupon/listuser")
    public BaseRespVo listuser(@Validated AdListBO adListBO, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return BaseRespVo.fail(666, "请输入正确的数据类型");
        }
        CouponListuserVO couponListuserVO = couponService.listuser(adListBO);
        return BaseRespVo.ok(couponListuserVO);
    }

    //编辑优惠券信息
    @PostMapping ("coupon/update")
    public BaseRespVo update(@RequestBody CouponCreateBO marketCoupon){
        if (marketCoupon.getEndTime().before(marketCoupon.getStartTime()) || marketCoupon.getStartTime().equals(marketCoupon.getEndTime())){
            return BaseRespVo.fail(404,"温馨提示：优惠券有限期的结束时间需在开始时间之后，请合理设置哦！");
        }
        MarketCoupon marketCoupon1 = couponService.update(marketCoupon);

        return BaseRespVo.ok(marketCoupon1);
    }

    //添加优惠券信息
    @PostMapping("coupon/create")
    public BaseRespVo create(@RequestBody CouponCreateBO marketCoupon){
        //在设置优惠券时间时，如果优惠券的接收时间在在开始时间之前热，弹出错误的信息
        if ((marketCoupon.getStartTime() != null) && (marketCoupon.getEndTime()!=null)) {
            if (marketCoupon.getEndTime().before(marketCoupon.getStartTime()) || marketCoupon.getStartTime().equals(marketCoupon.getEndTime())) {
                return BaseRespVo.fail(404, "温馨提示：优惠券有限期的结束时间需在开始时间之后，请合理设置哦！");
            }
        }
        // 如果设置了商品类型限制，goodsValue不能是空的
        Integer[] goodsValue = marketCoupon.getGoodsValue();
        if (marketCoupon.getGoodsType() == 1) {
            if (goodsValue == null || goodsValue.length == 0) {
                return BaseRespVo.fail(10000, "请选择优惠券的类目限制");
            }
        }
        if (marketCoupon.getGoodsType() == 2) {
            if (goodsValue == null || goodsValue.length == 0) {
                return BaseRespVo.fail(10000, "请选择优惠券的商品限制");
            }
        }

        MarketCoupon marketCoupon1 = couponService.create(marketCoupon);
        return BaseRespVo.ok(marketCoupon1);
    }
}
